package com.cms.controller;


import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cms.common.StringUtils;
import com.cms.constant.CmsConstant;
import com.cms.model.TrAdminUser;
import com.cms.model.TrCatalog;
import com.cms.service.DictService;
import com.cms.service.ReportService;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

@Controller
public class DictCatalogManagerController extends BaseController{

	 @Autowired
	 private DictService dictService; 
	 
	 @Autowired
	 private ReportService reportService;
	 
	 @RequestMapping("/dictCatalogManager.do")
	 public String enter(HttpServletRequest req,HttpServletResponse resp,ModelMap map)throws Exception
	 { 
		 return "dictCatalog/dictCatalogMain.html";
	 }
	 
 
	 @RequestMapping("/dictCatalogManager/dictCatalogList.do")
	 public String dictCatalogList(HttpServletRequest req,HttpServletResponse resp,ModelMap map)throws Exception
	 {
		 Object obj = this.reportService.getReportResult("pagingDictCatalogList", Collections.EMPTY_MAP);
		 map.put("dictCatalogList", obj);
		 return "dictCatalog/dictCatalogList.html";
	 }
	 
	  
	 
	 @RequestMapping("/dictCatalogManager/saveDictCatalog.do")
	 public void saveDictCatalog(HttpServletRequest req,HttpServletResponse resp,ModelMap map)throws Exception
	 {
	     String data = req.getParameter("data");
	     TrAdminUser tu = super.getLoginUser(req);
		 Map<String,Object>result = new HashMap<String, Object>();
	     if(!StringUtils.isBlank(data))
		 {
			JsonArray array=  new JsonParser().parse(data).getAsJsonArray();
			List<TrCatalog>catalogs = new ArrayList<TrCatalog>();
			TrCatalog catalog = null;
			JsonObject obj = null;
			for(int i=0;array!=null&&i<array.size();i++)
			{
				obj=array.get(i).getAsJsonObject();
				catalog = new TrCatalog();
				array.get(i).getAsJsonObject();
				catalog.setCreateUser(tu.getId());
				catalog.setModifyUser(tu.getId());
				catalog.setCatalogName(obj.get("name").getAsString());
				catalog.setCatalogDesc(obj.get("value").getAsString());
				catalog.setId(obj.get("id").getAsInt());
				catalogs.add(catalog);
			}
			dictService.saveDictCatalogs(catalogs);
		 }
	     result.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
	     this.renderText(resp, new Gson().toJson(result));
	 }
	 
	 
	 @RequestMapping("/dictCatalogManager/delDictCatalog.do")
	 public void delDictCatalog(HttpServletRequest req,HttpServletResponse resp,ModelMap map)throws Exception
	 { 
		 int catalogId = StringUtils.str2Int(req.getParameter("id"));
		 TrCatalog catalog =new TrCatalog();
		 catalog.setId(catalogId);
		 this.dictService.delCatalog(catalog);
		 Map<String,Object>result = new HashMap<String, Object>();
		 result.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
		 this.renderText(resp, new Gson().toJson(result));
	 }
}
